#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {

        if (matrix.size() < 1) {
            return;
        }

        if (matrix.size() != matrix[0].size()) {
            return;
        }

        int len = matrix.size();

        vector<int> m(len);
        for (int i=0; i<len/2; i++) {

            m = matrix[i];

            for (int j=i; j<len-1-i; j++) {
                int tmp = matrix[j][len-1-i] ;
                matrix[j][len-1-i] = m[j];
                m[j] = tmp;
            }

            for (int j=i; j<len-1-i; j++) {
                int tmp = matrix[len-1-i][j] ;
                matrix[len-1-i][j] = m[len-1-j];
                m[len-1-j] = tmp;
            }

            for (int j=i; j<len-1-i; j++) {
                int tmp = matrix[len-1-j][i] ;
                matrix[len-1-j][i] = m[j];
                m[j] = tmp;
            }

            for (int j=i; j<len-1-i; j++) {
                int tmp = matrix[i][j] ;
                matrix[i][j] = m[len-1-j];
                m[len-1-j] = tmp;
            }

        }

    }
};

int main() {
    vector<vector<int>> matrix = {
        {1,2,3},
        {4,5,6},
        {7,8,9}
    };

    Solution s;
    s.rotate(matrix);

    for (auto v : matrix) {
        for (auto i : v) {
            cout << i << "\t";
        }
        cout << endl;
    }
}

